<?php
/***
	用户模型。
**/
class User_model extends CI_Model {
	
	public function __construct() {
		$this->load->database();
	}
	//所有
	public function get_all() {
		
		$query = $this->db->get('users');
		return $query->result_array();
		
	}
	//由id返回一个用户
	public function get_by_id($id) {
		
		$query = $this->db->get_where('users', array('id' => $id));
		return $query->row_array();
		
	}
	//按position返回
	public function get_by_position ($position) {
		$sqlstring = "select * from users where position LIKE '%".$position."%'";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
	}
	//新增
	public function create () {
		
		$passwd = $this->input->post('passwd');
		$passwd = password_hash($passwd, PASSWORD_DEFAULT);
		
		$data = array(
			'id' => $this->input->post('id'),
			'name' => $this->input->post('name'),
			'pass_wd' => $passwd,
			'position' => $this->input->post('position')
			
		);
		$this->db->insert('users', $data);
	}
	
	public function ch_passwd ($user_id) {
		$new_passwd = $this->input->post('new_passwd');
		$new_passwd = password_hash($new_passwd, PASSWORD_DEFAULT);
		$data = array(
			'pass_wd' => $new_passwd	
		);
		
		$this->db->where('id', $user_id);
		$this->db->update('users', $data);
	}
	/*
	//只返回所有name
	public function get_name_all() {
		
		$sqlstring = "select name from clients";
		$query = $this->db->query($sqlstring);
		return $query->result_array();
		
	}
	
	
	//列出未校核客户
	public function get_uncheck() {
		
		$query = $this->db->get_where('clients', array('check' => 'NO'));
		return $query->result_array();
	}
	//列出潜在客户
	public function get_type() {
		
		$type = "potantial";
		$query = $this->db->get_where('clients', array('type' => $type));
		return $query->row_array();
	}
	//按行业类别搜索
	public function get_sector($sector = FALSE) {
		
		$query = $this->db->get_where('clients', array('sector' => $sector));
		return $query->row_array();
	}
	
	//按id查询
	public function get_by_id($id = FALSE) {
		
		$query = $this->db->get_where('clients', array('id' => $id));
		return $query->row_array();
	}
	
	
	
	//通过校核
	public function update_check ($id) {
		$data = array(
			'check' => 'YES'
		);
		
		$this->db->where('id', $id);
		$this->db->update('clients', $data);
	}
	
	//修改客户记录
	public function update_all ($id) {
		$data = array(
			'name' => $this->input->post('name'),
			'sector' => $this->input->post('sector'),
			'type' => $this->input->post('type'),
			'remark' => $this->input->post('remark'),
			'website' => $this->input->post('website'),
			'country' => $this->input->post('country'),
			'contact_by' => $this->input->post('contact_by'),
			'address' => $this->input->post('address'),
			'scope' => $this->input->post('scope')			
		);
		
		$this->db->where('id', $id);
		$this->db->update('clients', $data);
	}
	*/

}
